<template>
    <div>
        Mall Login Page <br>
        <form>
            username: <input type="text" v-model="username"> <br>
            password: <input type="text" v-model="password"> <br>
            <button type="button" @click="login">登录</button>
            <iframe ref="movieFrame" src="http://localhost:8082" style="height: 0px;width: 0px;display: none;"></iframe>
        </form>
    </div>
</template>

<script>
export default {
    data() {
        return {
            username: '',
            password: ''
        }
    },
    methods: {
        login() {
            let param = {
                username: this.username,
                password: this.password
            };
            this.$axios.post("http://localhost:8061/auth/login", param).then((response) => {
                if(response.data.code == 200) {
                    // 登录成功，获取后端发来的token
                    let token = response.data.data;
                    // 将token存入前端的localStorage中
                    localStorage.setItem("token", token);
                    // 跳回一开始就想去的url
                    this.$router.push(this.$store.getters.toUrl).catch(err => err);
                    // 传送token
                    let movieFrame = this.$refs.movieFrame;
                    movieFrame.contentWindow.postMessage(token,'*');
                } else {
                    alert("登录失败");
                }
                
            });
        }
    }
}
</script>

<style scoped>

</style>